package com.topscomm.file.convert.ffmpeg;

import java.io.File;
import java.util.List;

import com.topscomm.file.FileProperties;
import com.topscomm.pub.util.SecurityUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.common.ParameterCache;

import cn.hutool.core.io.FileUtil;
import lombok.Data;

/**
 * 
 * @description:
 * @author: donghang
 * @date: 2021年3月17日上午9:29:41
 * @modify:
 */
@Data
public abstract class AbstractFFmpegFileConvert {
	/**
	 * ffmpeg路径
	 */
	protected String ffmpegPath;
	/**
	 * enc路径
	 */
	protected String encPath;
	/**
	 * 切片间隔
	 */
	protected String duration;
	/**
	 * 根路径
	 */
	protected String rootPath;
	/**
	 * 播放相对路径
	 */
	protected String playRelativePath;
	protected String rootUrlTag = "${url}";

	public AbstractFFmpegFileConvert() {
		FileProperties fileProperties = (FileProperties) SpringUtil.getBean("fileProperties");
		this.ffmpegPath = fileProperties.getFfmpegPath();
		this.duration = fileProperties.getFfmpegDuration();
		this.rootPath = fileProperties.getStorePath();
		this.encPath = fileProperties.getEncPath();
		this.playRelativePath = fileProperties.getPlayPath();
	}

	/**
	 * 文件加密
	 * 
	 * @description:
	 * @param fileName
	 * @param prefix
	 * @author: donghang
	 * @date: 2021年3月17日上午9:45:51
	 * @modify:
	 */
	protected void fileEncrypt(String fileName, String prefix) {
		try {
			List<String> list = FileUtil.readLines(fileName, "UTF-8");
			StringBuilder sBuilder = new StringBuilder();
			list.forEach(m -> {
				if (m.endsWith(".ts")) {
					sBuilder.append(prefix + m + "\r\n");
				} else {
					sBuilder.append(m + "\r\n");
				}
			});
			String secretKey = ParameterCache.getValue("m3u8SecretKey", "12345678901234567890123456789012");
			byte[] secretContent = SecurityUtil.encryptDes(sBuilder.toString().getBytes(), secretKey);
			// byte[] secretContent = sBuilder.toString().getBytes();
			FileUtil.writeBytes(secretContent, new File(fileName), 0, secretContent.length, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
